package Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBFunction.AdminManager;
import DBFunction.GroupManager;
import DBFunction.UserManager;
import DTO.Admin;
import Parser.StringtoMD5;

/**
 * Servlet implementation class ServerAdminManager
 */
public class ServerAdminManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServerAdminManager() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if(action.compareTo("add")==0){
			String account = request.getParameter("account");
			String passwords = request.getParameter("passwords");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			Admin inputAdmin = new Admin(account,passwords,firstName,lastName);
			try {
				AdminManager.insert(inputAdmin);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(action.compareTo("delete")==0){
			String[] deleteList = request.getParameterValues("adminSelect");
			if (deleteList!=null){
				try {
					for (int x=0;x < deleteList.length;x++){
						AdminManager.deleteById(deleteList[x]);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ServerAdminDisplay");
		   dispatcher.forward( request, response );
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
